﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using BusinessLogic;

namespace Presentation
{
	public partial class ViewMain : Form, IViewMain
	{
		// ATTRIBUTS

		// Controlleur de vue
		private IControllerViewMain _oControllerView;
		public IControllerViewMain ControllerView
		{
			get { return _oControllerView; }
			set { _oControllerView = value; }
		}		
		
		// Champ saisi : Nom
		public string NomPersonne
		{
			get { return tbNom.Text; }
			set { tbNom.Text = value; }
		}
		
		// Champ saisi : Prénom
		public string PrenomPersonne
		{
			get { return tbPrenom.Text; }
			set { tbPrenom.Text = value; }
		}
		
		// Champ saisi : Age
		public int AgePersonne
		{
			get 
			{ 
				int nAge;
				string strAge = tbAge.Text;
				if( string.IsNullOrEmpty(strAge) || !int.TryParse(strAge, out nAge)	 )
					return 0;

				return nAge; 
			}
			set { tbAge.Text = value.ToString(); }
		}
		
		// Champ saisi : Adresse
		public string AdressePersonne
		{
			get { return tbAdresse.Text; }
			set { tbAdresse.Text = value; }
		}
		
		// Champ resultat : Nombre inscrit
		public int ResultNombreInscrit
		{
			set { tbInscrit.Text = value.ToString(); }
		}
		
		// Champ resultat : Age Moyen
		public double ResultMoyenneAge
		{			
			set { tbMoyenneAge.Text = value.ToString("0.00"); }
		}
		
		// Etat : Bouton Ajouter
		public bool EtatBtAjouter
		{
			get { return btAjouter.Enabled; }
			set 
			{ 
				if( value == true && btAjouter.Enabled == false )
				{
					btAjouter.Enabled = true;
				}
				else if( value == false && btAjouter.Enabled == true )
				{
					btAjouter.Enabled = false;
				}
			}
		}
		
		// Etat : Bouton Modifier
		public bool EtatBtModifier
		{
			get { return btModifier.Enabled; }
			set 
			{ 
				if( value == true && btModifier.Enabled == false )
				{
					btModifier.Enabled = true;
				}
				else if( value == false && btModifier.Enabled == true )
				{
					btModifier.Enabled = false;
				}
			}
		}

		// Etat : Bouton Supprimer		
		public bool EtatBtSupprimer
		{
			get { return btSupprimer.Enabled; }
			set 
			{ 				
				if( value == true && btSupprimer.Enabled == false )
				{
					btSupprimer.Enabled = true;
				}
				else if( value == false && btSupprimer.Enabled == true )
				{
					btSupprimer.Enabled = false;
				}
			}
		}

		// CONSTRUCTEURS
		public ViewMain()
		{
			InitializeComponent();
		}

		public ViewMain( IControllerViewMain oControllerView ) : this()
		{
			this._oControllerView	= oControllerView;						
		}


		// METHODES
		public void Initialize()
		{
			ShowDialog();
		}

		// METHODES GRID
		public void ClearGrid()
		{
			// Define columns in grid
			gridPersonne.Columns.Clear();

			gridPersonne.Columns.Add("Code",	0,	HorizontalAlignment.Left);
			gridPersonne.Columns.Add("Nom",		75,	HorizontalAlignment.Left);
			gridPersonne.Columns.Add("Prenom",	75,	HorizontalAlignment.Left);
			gridPersonne.Columns.Add("Age",		50,	HorizontalAlignment.Right);
			gridPersonne.Columns.Add("Adresse",	150,	HorizontalAlignment.Left);

			// Add rows to grid
			gridPersonne.Items.Clear();			
		}

		public void AddRowToGrid( IPersonneEntity oPersonne )
		{
			if( oPersonne == null )
				return;

			ListViewItem parent;
			parent = gridPersonne.Items.Add(oPersonne.Code.ToString());
			if( parent != null )
			{				
				parent.SubItems.Add(oPersonne.Nom);
				parent.SubItems.Add(oPersonne.Prenom);
				parent.SubItems.Add(oPersonne.Age.ToString());
				parent.SubItems.Add(oPersonne.Adresse);
			}			
		}

		public void UpdateRowInGrid( IPersonneEntity oPersonne )
		{
			if( oPersonne == null )
				return;

			ListViewItem oRowToUpdate = null;

			foreach (ListViewItem oRow in gridPersonne.Items)
			{
				if(oRow.Text == oPersonne.Code.ToString())
				{
					oRowToUpdate = oRow;
					break;
				}
			}

			if (oRowToUpdate != null)
			{
				oRowToUpdate.Text				= oPersonne.Code.ToString();			
				oRowToUpdate.SubItems[1].Text	= oPersonne.Nom;	
				oRowToUpdate.SubItems[2].Text	= oPersonne.Prenom;
				oRowToUpdate.SubItems[3].Text	= oPersonne.Age.ToString();
				oRowToUpdate.SubItems[4].Text	= oPersonne.Adresse;				
			}
		}

		public void RemoveRowInGrid( IPersonneEntity oPersonne )
		{
			if( oPersonne == null )
				return;

			ListViewItem oRowToRemove = null;

            foreach (ListViewItem oRow in gridPersonne.Items)
            {
                if( oRow.Text == oPersonne.Code.ToString() )
                {
                    oRowToRemove = oRow;
					break;
                }
            }

            if (oRowToRemove != null)
            {
                gridPersonne.Items.Remove(oRowToRemove);
                gridPersonne.Focus(); // MAJ Grid
            }
		}

		public string GetIdRowSelected()
		{
			if (gridPersonne.SelectedItems.Count > 0)
                return gridPersonne.SelectedItems[0].Text;
            else
                return "";
		}

		public void SetRowSelected( IKeyEntity oCode )
		{
			if( oCode != null )
			{
				foreach (ListViewItem oRow in gridPersonne.Items)
				{
					if( oRow.Text == oCode.ToString() )
					{
						oRow.Selected = true;
						gridPersonne.Focus(); // MAJ Grid
					}
				}
			}
			else
			{
				gridPersonne.SelectedItems.Clear();
			}
		}
		
		// EVENTS BOUTON
		private void btAjouter_Click(object sender, EventArgs e)
		{
			ControllerView.AddPersonne();
		}

		private void btModifier_Click(object sender, EventArgs e)
		{
			ControllerView.UpdatePersonne();
		}

		private void btSupprimer_Click(object sender, EventArgs e)
		{
			ControllerView.DeletePersonne();
		}		

		private void btAnnuler_Click(object sender, EventArgs e)
		{
			ControllerView.Cancel();
		}	

		// EVENT GRID		
		private void gridPersonne_SelectedIndexChanged(object sender, EventArgs e)
		{
			string strCodeSelected = "";
			if (gridPersonne.SelectedItems.Count > 0)
				strCodeSelected = gridPersonne.SelectedItems[0].Text;
			
			ControllerView.SelectedRowChanged( strCodeSelected );
		}

			
	}

}
